package com.guohang.ESProject08;

import java.io.FileOutputStream;
import java.util.List;

import android.util.Log;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class CreatePDF {
	

    public static final String RESULT = "mnt/sdcard/image_direct.pdf";
   
    
    public CreatePDF(){}
	
	public void PDF(List<Capture> story){
	// step1: creation of a document-object
    try{
        Document document
        = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
        
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        
            writer.setCompressionLevel(0);
            document.open();
            
            //step3: insert capture: img, text, sensor stuff
            //loop the data List
    		for(int i=0; i < story.size(); i++){
    			Capture capture = story.get(i);
    			
    			if(capture != null){
       
    			//get and add image
    			 if(capture.getPath() != null){
    			    Image img = Image.getInstance(capture.getPath());
    			    img.setAlignment(Image.ALIGN_CENTER);
    	            img.setBorder(Image.BOX);
    	            img.setBorderWidth(10);
    	            img.setBorderColor(BaseColor.LIGHT_GRAY);
    	          //img.scaleToFit(1000, 72);
    	            document.add(img);
    	        //new line
    	            document.add(Chunk.NEWLINE);
    			 }
    			  //img.setAbsolutePosition((PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,(PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
    	          // writer.getDirectContent().addImage(img);
    	        
    	        //now get text
    			 if(capture.getText() != null){
    	           Paragraph p = new Paragraph(capture.getText(), new Font(FontFamily.HELVETICA, 12));
    	           p.setAlignment(Element.ALIGN_CENTER);
    	           document.add(p);
    			 }
    			}
    		}

		//wrap up
        document.close();
        Log.d("OK", "done");
    } 
    
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /*
    catch (DocumentException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (IOException e)
    {
    	e.printStackTrace();
    }  */
    
	}
}
